
load("js/v6/hxb/hxb_credit.js")
load("js/v6/hxb/hxb_debit.js")
load("js/v6/hxb/hxb_comm.js")

var v6_hxb_java_funcs = {
    openHomePage : function(){
        //访问首页
        get("https://sbank.hxb.com.cn/easybanking/jsp/indexComm.jsp");
        var indexHtml = luaFuncs.getHtml();
        //页面不为空
        context.put("indexHtml", indexHtml)
        storeDataToFile(context, indexHtml, "index.html")
    },
    checkProxyAvailable: function () {
        var indexHtml = context.get("indexHtml")
        if(StringUtils.isBlank(indexHtml)) {
            return false
        }
        var doc = Jsoup.parse(indexHtml)
        logger.info("任务【 {} 】标题='{}'", context.get(LUA_TASK_KEY), doc.select("title").text())
        logger.info("任务【 {} 】id='div1'的元素个数{}", context.get(LUA_TASK_KEY), doc.select("#div1").size())

        return equals(doc.select("title").text(), "华夏银行 - 个人登录页") && doc.select("#div1").size() > 0

    },
    checkTargetWebsiteAvailable: function(){ return true },
    checkControlAvailable:function (){ return luaFuncs.checkControlAvailable()},
    login: function () {
        show();
        var clickElementId = equals(USERNAME, LoginType.stringToLoginType(context.get(LUA_TASK_LOGIN_TYPE))) ? "divli1" : "divli2"
        logger.info("任务【 {} 】点击元素ID {}", context.get(LUA_TASK_KEY), clickElementId)
        click_by_id(clickElementId)
        var inputResult = luaFuncs.inputUPAndGetOperationResult(getLoginParams(context)[0], context.get(LUA_TASK_TOKEN), clickElementId);
        hide();

        var isPasswordCorrectFunc = function(inputResult) {
            return Boolean.valueOf(inputResult)
        }

        var getPasswordErrorMsgFunc = function(inputResult) {
            return "密码因控件输入错误"
        }

        assertTrueOrThrowException(inputResult, isPasswordCorrectFunc, getPasswordErrorMsgFunc)
        logger.info("{}, 点击登录按钮", context.get(LUA_TASK_KEY));
        var loginResult = luaFuncs.click_by_name("qy_sut");
        logger.info("{}, 登录成功返回的结果:{}", context.get(LUA_TASK_KEY),loginResult);

        //check开始: 检查图片验证码位数是否正确
        var isLoginCorrectFunc = function(loginResult) {
            return StringUtils.equals(loginResult, "OK")
        }

        var getLoginErrorMsgFunc = function(loginResult) {
            return loginResult
        }


         assertTrueOrThrowException(loginResult, isLoginCorrectFunc, getLoginErrorMsgFunc)
           /*var cookies = luaFuncs.getLoginCookie();
            context.put("cookies", cookies);*/
    },
    crawl: function () {
        var htmls = ajax_get("https://sbank.hxb.com.cn/easybanking/PAccountInfoDetailsQuery/PAccountInfoDetailsQueryAction.do?actionType=entry&OLN_top_activeNode=top11&OLN_top11side10_activeNode=top10side30&sideMenuBar=OLN_top11side10");
        storeDataToFile(context, htmls, "cardList.html");
        var doc = Jsoup.parse(htmls);
        var tableElement = doc.getElementById("accountDetailQueryDisplayVBList_row");
        if (tableElement == null) {
            throw new BankException(BALO_22101_10, "尚未注册或无此用户");
        }
        //存放信用卡、借记卡对象
        var cardList = new ArrayList();
        var tbodys = tableElement.select("tbody");
        var trs1 = tbodys.select("tr");

        for (var q = 0; q < trs1.size(); q++) {
            var tds = trs1.get(q).select("td");
            var currentType = tds.get(4).text();
            if (equals(currentType, "华夏信用卡")) {
                logger.info("任务taskKey = {}, 开始抓取信用卡", context.get(LUA_TASK_KEY));
                //华夏信用卡抓取\解析主方法
                var creditCardAllData=hxb_credit_handle.hxb_credit_main(q, tds);
                cardList.add(creditCardAllData);
                break;
            }

            if (equals(currentType, "活期")) {
                logger.info("任务taskKey = {}, 开始抓取借记卡", context.get(LUA_TASK_KEY));

                //华夏借记卡抓取\解析主方法
                var debitCardAllData=hxb_debit_handle.hxb_debit_main(q, tds);
                cardList.add(debitCardAllData);
            }
        }

        context.put(ALL_DATA, cardList);

    },
    exitLogin: function () {}
}

var luaFuncs = {
    checkControlAvailable:function(){
        var map = new HashMap();
        map.put("method","checkControlAvailable");
        return sendAndReceive(map)
    },
    getHtml:function () {
        var map = new HashMap();
        map.put("method","getHtml");
        return sendAndReceive(map);
    },
    click_by_name: function () {
        var map = new LinkedHashMap();
        map.put("method", "click_by_name");
        //lp.Send(JSON.toJSONString(map));
        return sendAndReceive(map)
    },

    openLoginPage: function () {
        var map = new LinkedHashMap();
        map.put("method", "openLoginPage");
        return sendAndReceive(map)
    },

    inputUPAndGetOperationResult: function (username, password, type) {
        var map = new LinkedHashMap();
        map.put("method", "inputUPAndGetOperationResult");
        map.put("username", username);
        map.put("password", password);
        map.put("type", type);

        return sendAndReceive(map)
    },

    /**
     * 输入密码
     *
     * @param password
     * @return
     */
    input_password_hx: function (password) {
        var map = new LinkedHashMap();
        map.put("method", "input_password_hx");
        map.put("password", password);
        if (map.containsKey("password")) {
            map.put("password", "*******");
        }

        return sendAndReceive(map)
    },

    check_is_img: function () {
        var map = new LinkedHashMap();
        map.put("method", "check_is_img");
        return sendAndReceive(map)
    }
}